namespace Warehouse

open System.Windows.Data
open Warehouse.Controls

open FSharp.Windows

[<AbstractClass>]
type UpcSearchModel() = 
    inherit Model()

    abstract ProductSearch : ProductSearchModel with get, set
    abstract SelectedUPC : UpcModel with get, set    

type UpcSearchView() as this = 
    inherit StaticView<UpcSearchModel, UPCSearchWindow>()

    do 
        this.DefaultOKButton <- this.Control.OK
    
    override this.SetBindings model =
        Binding.FromExpression 
            <@ 
                this.Control.UPCs.ItemsSource <- model.ProductSearch.SelectedProduct.UPCs
                this.Control.UPCs.SelectedItem <- model.SelectedUPC
            @>

        Binding.FromExpression 
            <@ 
                this.Control.OK.IsEnabled <- isNotNull model.SelectedUPC 
            @>

type UpcSearchController(repository : DataModel.Repository) = 
    inherit StaticViewController<UpcSearchModel>()

    override this.InitModel model = 
        model.ProductSearch <- Model.Create()

    static member Mvc(model, repository) =
        let view = UpcSearchView()
        Mvc(model, view, UpcSearchController(repository))
            .Compose(
                ProductSearchChildController(repository), 
                ProductSearchPartialView(view.Control.ProductSearch), 
                fun m -> m.ProductSearch)
               
